﻿using System;
using System.IO;
using NMock2.Monitoring;

namespace NMock2.Events
{
	/// <summary>
	/// The result of a HookUp expectation
	/// </summary>
	/// <remarks>
	/// This class is included in the NMock library to keep it together and complete.
	/// 
	/// It is, however, obsolete and a newer construct should be used.
	/// 
	/// Consider <see cref="MockEventInvoker"/>.
	/// 
	/// 
	/// 
	/// Code by Eric Willeke -- Keep my name here, please :) 
	/// http://manicprogrammer.com/cs/blogs/willeke/archive/2007/10/17/testing-delegates-with-nunit-and-nmock.aspx
	/// Adapted from original code by Neil Bourgeois - Edmonton, Alberta, Canada
	/// http://becomingagile.blogspot.com/2007/03/raising-events-in-nmock-20.html
	/// </remarks>
	[Obsolete("Consider using a MockEventInvoker to handle events.")]
	public class MockEventHookup<T> : IAction where T : class
	{
		private readonly MockEvent<T> mockEvent;

		public MockEventHookup(MockEvent<T> mockEvent)
		{
			this.mockEvent = mockEvent;
		}

		public void Invoke(Invocation invocation)
		{
			T handler = invocation.Parameters[0] as T;

			if (handler == null)
			{
				throw new Exception(
					string.Format(
						"Unknown event handler type. {0} was expected.",
						typeof(T).ToString()));
			}

			mockEvent.Initialize(handler);
		}

		public void DescribeTo(TextWriter writer)
		{
			// do nothing
		}

	}
}
